<template>
  <el-row style="width: 100%">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >查看部门</el-dropdown-item
              >
              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      require: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async handleCommand (type) {
      switch (type) {
        case 'add': {
          // console.log(type)
          // 向父组件传值
          this.$emit('adddepart', this.treeNode.id)
          break
        }
        // 编辑事件
        case 'edit': {
          // 通知父组件编辑事件
          this.$emit('editDepar', this.treeNode.id)
          break
        }
        // 删除事件
        case 'del': {
          // 点击了删除事件,交互框
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async () => {
              await delDepartments(this.treeNode.id)
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              // 删除成功后重新请求数据刷新页面
              this.$emit('delSuccess')
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            })
          break
        }
      }
    }
  }
}
</script>

<style></style>
